package owlapps2012.owlToSql.base;

import java.io.FileInputStream;
import java.util.Properties;

/**
* Author: Piotr Nowara<br>
* piotrnowara@gmail.com<br>
* code made available under Mozilla Public License (http://www.mozilla.org/MPL/MPL-1.1.html)<br>
* copyright 2013, Piotr Nowara<br>
*/
public class DynamicDbAppConfig extends DynamicAppConfig
{
	private static final String DB_USER_PARAM_NAME = "DbUser";
	private static final String DB_SRC_SCHEMA_PARAM_NAME = "DbSrcSchema";
	private static final String DB_APP_SCHEMA_PARAM_NAME = "DbAppSchema";
	private static final String DB_CONN_URL_PARAM_NAME = "DbConnURL";
	private static final String DB_DRIVER_PARAM_NAME = "DbDriver";
	private static final String DB_CATEGORY_SEQUENCE = "CategoryTableSQLSequence";
	private static final String DB_CATEGORY_TYPE_SEQUENCE = "CategoryTypeTableSQLSequence";
	
	protected String dbUserName;
	protected String dbSrcSchema;
	protected String dbAppSchema;
	protected String dbDriverName;
	protected String dbConnURL;
	protected String dbCategorySequenceName;
	protected String dbCategoryTypeSequenceName;

	protected String configFilePath;
	protected Properties dbConfig = new Properties();

	public DynamicDbAppConfig(String appConfigFilePath, String dbConfigFilePath) throws Exception 
	{
		super(appConfigFilePath);
		this.dbConfig.load(new FileInputStream(dbConfigFilePath));
		this.initDbConf();
	}

	private void initDbConf() throws Exception
	{
		this.dbUserName = this.dbConfig.getProperty(DB_USER_PARAM_NAME);
		if(this.dbUserName == null)
			throw new Exception("Config file does not specify database user name!");
		this.dbSrcSchema = this.dbConfig.getProperty(DB_SRC_SCHEMA_PARAM_NAME);
		if(this.dbSrcSchema == null)
			throw new Exception("Config file does not specify source database schema name!");
		this.dbDriverName = this.dbConfig.getProperty(DB_DRIVER_PARAM_NAME);
		if(this.dbDriverName == null)
			throw new Exception("Config file does not specify JDBC driver!");
		this.dbConnURL = this.dbConfig.getProperty(DB_CONN_URL_PARAM_NAME);
		if(this.dbConnURL == null)
			throw new Exception("Config file does not specify JDBC connection URL!");
		this.dbAppSchema = this.dbConfig.getProperty(DB_APP_SCHEMA_PARAM_NAME, "LINKEDFACTS");
		this.dbCategorySequenceName = this.dbConfig.getProperty(DB_CATEGORY_SEQUENCE, "CATEGORY_SEQ");
		this.dbCategoryTypeSequenceName = this.dbConfig.getProperty(DB_CATEGORY_TYPE_SEQUENCE, "CAT_TYPE_SEQ");
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public String getDbSrcSchema() {
		return dbSrcSchema;
	}

	public String getDbAppSchema() {
		return dbAppSchema;
	}

	public String getDbDriverName() {
		return dbDriverName;
	}

	public String getDbConnURL() {
		return dbConnURL;
	}

	public String getDbCategorySequenceName() {
		return dbCategorySequenceName;
	}

	public Properties getDbConfig() {
		return dbConfig;
	}

	public String getDbCategoryTypeSequenceName() {
		return dbCategoryTypeSequenceName;
	}
	
}
